package controleur;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashMap;

import javax.swing.JFileChooser;

import vue.PanneauPrincipal;

import model.Coordonnee;
import model.Environnement;
import model.Cellule;

public class ChargeGrille implements ActionListener {
	private Environnement env = null;
	private PanneauPrincipal fenetre = null;
	public ChargeGrille(Environnement env, PanneauPrincipal panPrinc) {
		this.env = env;
		this.fenetre = panPrinc;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JFileChooser defautChooser = new JFileChooser();
		defautChooser.showOpenDialog(null);
		
		File f = defautChooser.getSelectedFile();
		if(f == null)
			return;
		ObjectInputStream obj = null;
		
		try {
			obj = new ObjectInputStream(new FileInputStream(f));
			@SuppressWarnings("unchecked")
			HashMap<Coordonnee, Cellule> hashMap = (HashMap<Coordonnee, Cellule>) obj.readObject();
			int hauteur = obj.readInt();
			int largeur = obj.readInt();
			env.loadMap(hashMap, hauteur, largeur);
			fenetre.reInit(hauteur, largeur, env);
			obj.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}


}
